package com.pai4j.message.config;

import com.pai4j.common.enums.InteractionMessageTypeEnum;
import com.pai4j.message.builder.interaction.*;
import com.pai4j.message.core.interceptor.InterceptorChain;
import com.pai4j.message.core.interceptor.MessageInterceptor;
import com.pai4j.message.core.provider.ResourceRegistry;
import com.pai4j.message.factory.MessageBuilderFactory;
import com.pai4j.message.factory.MessageSenderFactory;
import com.pai4j.message.factory.TemplateEngineFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import jakarta.annotation.PostConstruct;
import java.util.List;

/**
 * 消息SDK配置类
 * 自动扫描和注册各种组件
 *
 * @author PAI4J Team
 * @date 2025-11-01
 */
@Slf4j
@Configuration
@ComponentScan("com.pai4j.message")
@EnableConfigurationProperties({MessageProperties.class, EmailProperties.class, AggregationProperties.class})
@PropertySource(value = "classpath:application-pai4j-message-defaults.yml", factory = YamlPropertySourceFactory.class, ignoreResourceNotFound = true)
public class MessageConfiguration {

    @Autowired(required = false)
    private List<MessageInterceptor> interceptors;
    
    @Autowired
    private MessageProperties messageProperties;

    /**
     * 注册所有MessageBuilder到Factory
     */
    @PostConstruct
    public void registerMessageBuilders() {
        // 注册互动消息构建器
        MessageBuilderFactory.register(InteractionMessageTypeEnum.LIKE, LikeMessageBuilder.class);
        MessageBuilderFactory.register(InteractionMessageTypeEnum.COMMENT, CommentMessageBuilder.class);
        MessageBuilderFactory.register(InteractionMessageTypeEnum.REPLY, ReplyMessageBuilder.class);
        MessageBuilderFactory.register(InteractionMessageTypeEnum.COLLECT, CollectMessageBuilder.class);
        MessageBuilderFactory.register(InteractionMessageTypeEnum.FOLLOW, FollowMessageBuilder.class);
        
        log.info("注册MessageBuilder成功: count=5");
    }

    /**
     * 创建拦截器链
     */
    @Bean
    public InterceptorChain interceptorChain() {
        InterceptorChain.Builder builder = InterceptorChain.builder();
        
        if (interceptors != null && !interceptors.isEmpty()) {
            builder.addInterceptors(interceptors);
            log.info("注册消息拦截器: count={}", interceptors.size());
        } else {
            log.info("未发现消息拦截器");
        }

        return builder.build();
    }

    /**
     * 初始化完成后的日志和检查
     */
    @org.springframework.context.event.EventListener(
            org.springframework.context.event.ContextRefreshedEvent.class)
    public void onApplicationReady() {
        log.info("=================================");
        log.info("PAI4J Message SDK 初始化完成");
        log.info("=================================");
        
        // 检查是否至少有一个可用的发送器
        String defaultChannel = messageProperties.getDefaultChannel();
        if (MessageSenderFactory.getSender(defaultChannel) == null) {
            log.warn("当前的发送器[{}] ", defaultChannel);
         
        }
        
        // 打印注册信息
        log.info(ResourceRegistry.getRegistryInfo());
        log.info(MessageSenderFactory.getFactoryInfo());
        log.info(TemplateEngineFactory.getFactoryInfo());
        log.info(MessageBuilderFactory.getFactoryInfo());
        log.info("=================================");
    }
}

